package com.ruoyi.leave.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.leave.domain.WorkflowWaterPlan;
import com.ruoyi.leave.service.IWorkflowWaterPlanService;
import com.ruoyi.manage.domain.dto.Plan;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.ruoyi.common.constant.CommonResult.DEFAULT_NUM;
import static com.ruoyi.common.constant.CommonResult.DEFAULT_PAGESIZE;

/**
 * 用水申请Controller
 *
 * @author ruoyi
 * @date 2023-12-22
 */
@RestController
@RequestMapping("/workFlow/waterPlan")
public class WorkflowWaterPlanController extends BaseController {
    @Autowired
    private IWorkflowWaterPlanService workflowWaterPlanService;

    /**
     * 查询用水申请列表
     */
    @GetMapping("/planReviewList")
    public TableDataInfo list(WorkflowWaterPlan workflowWaterPlan) {
        //startPage();
        //List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        List<WorkflowWaterPlan> list = workflowWaterPlanService.selectWorkflowWaterPlanList(workflowWaterPlan);
                //stream().filter(item -> roles.stream().anyMatch(x -> x.getRoleName().equals(item.getRolegroup()))).collect(Collectors.toList());
        if (StringUtils.isBlank(workflowWaterPlan.getPageNum())) {
            workflowWaterPlan.setPageNum(DEFAULT_NUM + "");
        }
        if (StringUtils.isBlank(workflowWaterPlan.getPageSize())) {
            workflowWaterPlan.setPageSize(DEFAULT_PAGESIZE + "");
        }
        list = list.stream().skip((Integer.parseInt(workflowWaterPlan.getPageNum()) - 1) * Integer.parseInt(workflowWaterPlan.getPageSize())).limit(Integer.parseInt(workflowWaterPlan.getPageSize())).collect(Collectors.toList());
        return getDataTable(list);
    }

    /**
     * 提交申请
     */
    @Log(title = "提交申请", businessType = BusinessType.UPDATE)
    @PostMapping("/updatePlanSP")
    public AjaxResult updatePlanSP(@RequestBody Plan plan) {
        int i = workflowWaterPlanService.updatePlanSP(plan);
        if (0 == i) {
            return AjaxResult.error("年计划记录不存在!!!");
        } else if (2 == i) {
            return AjaxResult.error("请勿重复提交!!!");
        }
        return toAjax(i);
    }

    /**
     * 通过申请
     */
    @Log(title = "通过申请", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/completePlanSP/{planId}")
    public AjaxResult completePlanSP(@PathVariable("planId") Long planId) {
        int i = workflowWaterPlanService.completeTaskByPlanID(planId);
        return toAjax(i);
    }


    /**
     * 驳回申请
     */
    @Log(title = "驳回申请", businessType = BusinessType.UPDATE)
    @PostMapping("/rejectPlanSP")
    public AjaxResult rejectPlanSP(@RequestBody Plan plan) {
        int i = workflowWaterPlanService.rejectTaskByPlanID(plan);
        return toAjax(i);
    }

    /**
     * 审批
     */
    @Log(title = "审批", businessType = BusinessType.UPDATE)
    @PostMapping("/examinePlan")
    public AjaxResult examinePlan(@RequestBody Plan plan) {
        int i = workflowWaterPlanService.examinePlan(plan);
        return toAjax(i);
    }

    /**
     * 审批(农业)
     */
    @Log(title = "审批(农业)", businessType = BusinessType.UPDATE)
    @PostMapping("/examineAgriculturePlan")
    public AjaxResult examineAgriculturePlan(@RequestBody Plan plan) {
        int i = workflowWaterPlanService.examineAgriculturePlan(plan);
        return toAjax(i);
    }

}
